// Copyright (C) 2008-2012 Jeremy Hollingsworth
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using UnityEngine;
using UnityEditor;
using System;

public class GUIXDesignerWindow : EditorWindow
{   
    public GUIX.Designer.DesignerController _designerController;

    private bool _hasInitialized;

    public GUIXDesignerWindow()
        : base()
    {        
    }

    public void Init()
    {
        this.title = "GUIX Designer";
        _designerController = new GUIX.Designer.DesignerController();                
        _designerController.Position = this.position;
        _designerController.Init();
        _designerController.FocusWindowCallback += FocusMe;
        
        _hasInitialized = true;
    }

    void OnGUI()
    {
        if ( !_hasInitialized ) Init();

        if ( _designerController != null )
        {
            _designerController.Position = position;
            _designerController.Update();
        }
    }

    void Update()
    {
        // Close if we have been cleared on scene play already (make sure we have initialized first).
        if ( _hasInitialized && _designerController == null )
        {
            this.Close();
        }

        this.Repaint();
    }

    void CloseWindow()
    {        
        this.Close();
    }

    void OnDisable()
    {
        SaveWindowRect( this.position );
        _hasInitialized = false;

        if ( _designerController != null )
        {            
            _designerController.HandleClose();
        }
    }
          
    public void SaveWindowRect( Rect _position )
    {
        EditorPrefs.SetFloat( "GUIXDesigner Window PosX", _position.x );
        EditorPrefs.SetFloat( "GUIXDesigner Window PosY", _position.y );
        EditorPrefs.SetFloat( "GUIXDesigner Window Width", _position.width );
        EditorPrefs.SetFloat( "GUIXDesigner Window Height", _position.height );
    }

    public bool IsWindowRectSaved()
    {
        if ( EditorPrefs.HasKey( "GUIXDesigner Window PosX" ) && 
            EditorPrefs.HasKey( "GUIXDesigner Window PosY" ) && 
            EditorPrefs.HasKey( "GUIXDesigner Window Width" ) && 
            EditorPrefs.HasKey( "GUIXDesigner Window Height" ) )
        {
            return true;
        }

        return false;
    }

    public Rect LoadWindowRect()
    {
        if ( IsWindowRectSaved() )
        {
            return new Rect( EditorPrefs.GetFloat( "GUIXDesigner Window PosX" ), EditorPrefs.GetFloat( "GUIXDesigner Window PosY" ), EditorPrefs.GetFloat( "GUIXDesigner Window Width" ), EditorPrefs.GetFloat( "GUIXDesigner Window Height" ) );
        }
        else
        {
            return position;
        }
    } 

    public void FocusMe()
    {
        Focus();
    }   
}